<template>
  <el-dialog :close-on-click-modal='false' v-drag :title="type === '编辑'?$t('编辑'):$t('添加')" :v-if="dialogVisible" :visible.sync="dialogVisible" :width="lang==='zh'?'550px':'630px'" @close='close'>
    <div>
      <el-form ref="form" :model="record" :label-width="lang==='zh'?'140px':'180px'" label-position="left" :rules="rules" style="margin:0 20px;">
        <el-form-item :label="$t('接口名称')" prop="ifname">
          <el-select v-model="record.ifname" ref="isisIfname" filterable size='small' :placeholder="$t('请选择')" style="width:280px" :disabled="type === '编辑'">
            <el-option v-for="item in ifnameList" :disabled="isisList.includes(item.strPhysicsName) || item.enPortMode !== 0 || !item.ipv4 || !item.ipv4[0].cPortIpv4" :key="item.strPhysicsName" :label="item.strPhysicsName" :value="item.strPhysicsName">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item :label="$t('IsIs认证方式')" prop="interface_auth">
          <el-radio-group v-model="record.interface_auth" size='small'>
            <el-radio label="clear" class="isisClear">{{$t('明文')}}</el-radio>
            <el-radio label="md5" class="isisMd5">MD5</el-radio>
            <el-radio label="off" class="isisOff">{{$t('关闭认证')}}</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item :label="$t('接口认证密码')" prop="password" v-if="record.interface_auth !== 'off'">
          <template slot="label">{{$t('接口认证密码')}}
            <el-tooltip class="item" effect="light" :content="$t('提示信息:字符串，1-15个字符')" placement="top">
              <i class="el-icon-warning-outline" style="color:#666;font-size:14px;"></i>
            </el-tooltip>
          </template>
          <el-input v-emoji v-model="record.password" ref="isisPassword" style="width:280px" :placeholder="$t('请输入密码')" size='small'></el-input>
        </el-form-item>
        <el-form-item :label="$t('hello定时器')" prop="hello_interval">
          <template slot="label">{{$t('hello定时器')}}
            <el-tooltip class="item" effect="light" :content="$t('提示信息:无符号整数，(1-600)，默认值10s。设置Hello数据包的发送周期。')" placement="top">
              <i class="el-icon-warning-outline" style="color:#666;font-size:14px;"></i>
            </el-tooltip>
          </template>
          <el-input v-emoji v-model="record.hello_interval" ref="isisHelloInterval" style="width:280px" size='small'></el-input>
        </el-form-item>
        <el-form-item :label="$t('优先级')" prop="pri">
          <template slot="label">{{$t('优先级')}}
            <el-tooltip class="item" effect="light" :content="$t('提示信息:整数，(0-127)，默认值为1')" placement="top">
              <i class="el-icon-warning-outline" style="color:#666;font-size:14px;"></i>
            </el-tooltip>
          </template>
          <el-input v-emoji v-model="record.pri" ref="isisPri" style="width:280px" size='small'></el-input>
        </el-form-item>
      </el-form>
    </div>
    <span slot="footer" class="dialog-footer">
      <el-button type="primary" @click="submit" class="isisAddSubmit">{{$t('确 定')}}</el-button>
      <el-button @click="close" class="isisAddClose">{{$t('取 消')}}</el-button>
    </span>
  </el-dialog>
</template>
<script>
import { getPhysicalApiArray } from '@/api/network/api/physical'
import { getIsisIfconfig, setIsisIfconfig, addIsisIfconfig } from '@/api/network/route/dynamicRoute/ISIS'
import { inputClass, selectClass } from '@/utils/input-class.js'
export default {
  name: 'RipDialog',
  data () {
    var validateName = (rule, value, callback) => {
      if (String(value).trim() === '') {
        callback(new Error(this.$t('不能为空')))
      } else {
        callback()
      }
    }
    var validatePassword = (rule, value, callback) => {
      if (String(value).trim() === '') {
        callback(new Error(this.$t('不能为空')))
      } else if (String(value).length > 15) {
        callback(new Error(this.$t('长度不能超过15个字符')))
      } else {
        callback()
      }
    }
    var validateHelloInterval = (rule, value, callback) => {
      if (String(value).trim() === '') {
        callback()
      } else if (isNaN(Number(value))) {
        callback(new Error(this.$t('请输入数字')))
      } else if (!this.isInteger(value)) {
        callback(new Error(this.$t('只能输入整数')))
      } else if (Number(value) < 1 || Number(value) > 600) {
        callback(new Error(this.$t('必须在1-600之间')))
      } else {
        callback()
      }
    }
    var validatePri = (rule, value, callback) => {
      if (String(value).trim() === '') {
        callback()
      } else if (isNaN(Number(value))) {
        callback(new Error(this.$t('请输入数字')))
      } else if (!this.isInteger(value)) {
        callback(new Error(this.$t('只能输入整数')))
      } else if (Number(value) < 0 || Number(value) > 127) {
        callback(new Error( this.$t('必须在0-127之间')))
      } else {
        callback()
      }
    }

    return {
      dialogVisible: false,
      record: {
        interface_auth: 'off',
        ifname: '',
        hello_interval: '10',
        pri: '1',
        key_chain: '',
        key_id: '',
        password: ''
      },
      recordCopy: {
        interface_auth: 'off',
        ifname: '',
        hello_interval: '10',
        pri: '1',
        key_chain: '',
        key_id: '',
        password: ''
      },
      type: '',
      ifnameList: [],
      isisList: [],
      rules: {
        ifname: [{ required: false, validator: validateName, trigger: 'blur' }],
        interface_auth: [{ required: false, validator: validateName, trigger: 'blur' }],
        password: [{ required: false, validator: validatePassword, trigger: 'blur' }],
        hello_interval: [{ required: false, validator: validateHelloInterval, trigger: 'blur' }],
        pri: [{ required: false, validator: validatePri, trigger: 'blur' }]
      }
    }
  },
  computed: {
    change () {
      const { interface_auth } = this.record
      return { interface_auth }
    }
  },
  watch: {
    change: {
      handler (newValue) {
        if (newValue.interface_auth !== 'off') {
          this.$nextTick(() => {
            const data = ['isisPassword']
            inputClass(this.$refs, data)
          })
        }
      },
      deep: true,
      immediate: true
    }
  },
  methods: {
    init (data, type) {
      this.dialogVisible = true
      getPhysicalApiArray().then(res => { // strPhysicsName
        if (res.status === 0) {
          this.ifnameList = res.info
        }
      })
      this.isisList = []
      getIsisIfconfig().then(res => { // strPhysicsName
        if (res.status === 0) {
          for (let item of res.info) {
            this.isisList.push(item.ifname)
          }
        }
      })
      this.type = type
      if (data) {
        this.record.interface_auth = data.mode_info.interface_auth
        this.record.ifname = data.ifname
        this.record.hello_interval = data.hello_interval
        this.record.pri = data.pri
        if (this.record.interface_auth !== 'off') {
          this.record.password = data.mode_info.password
        }
      }
      this.$nextTick(() => {
        const data = ['isisHelloInterval', 'isisPri']
        const selectData = ['isisIfname']
        inputClass(this.$refs, data)
        selectClass(this.$refs, selectData)
      })
    },
    submit () {
      this.$refs.form.validate((valid) => {
        if (valid) {
          if (this.type === '编辑') {
            setIsisIfconfig(this.record).then(res => {
              if (res.status === 0) {
                this.$messages.success(this.$t('执行成功'))
                this.close()
                this.$parent.isisConfigListInfo()
              }
            })
          } else {
            addIsisIfconfig(this.record).then(res => {
              if (res.status === 0) {
                this.$messages.success(this.$t('执行成功'))
                this.close()
                this.$parent.isisConfigListInfo()
              }
            })
          }
        }
      })
    },
    close () {
      this.type = ''
      this.dialogVisible = false
      this.record = Object.assign({}, this.recordCopy)
      this.$refs.form.resetFields()
      this.ifnameList = []
      this.isisList = []
    },
    isInteger (value) {
      return (!isNaN(value) && (parseInt(value).toString().length === String(Number(value)).length))
    }
  }
}
</script>
